package cn.cdeden.knowledge.domain.dto;

import cn.cdeden.knowledge.domain.EnterprisePerm;
import io.github.linpeilie.annotations.AutoMapper;
import io.github.linpeilie.annotations.AutoMappers;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
* @author Chen
* @date 2022-10-13
*/
@Data
@Builder
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@AutoMappers({@AutoMapper(target = EnterprisePerm.class)})
public class EnterprisePermDto implements Serializable {
    private static final long serialVersionUID = 1L;

//    @ApiModelProperty(value = "编号")
    private Long id;
    private List<Long> ids;
//    @ApiModelProperty(value = "树节点编号")
    private Long treeNodeId;

//    @ApiModelProperty(value = "树节点编号")
    private List<Long> typeIds;
//    @ApiModelProperty(value = "类型")
    private Integer type;
//    @ApiModelProperty(value = "父亲节点id")
    private Long parentTreeNodeId;

//    @ApiModelProperty(value = "类型编号")
    private Long typeId;

//    @ApiModelProperty(value = "类型名称")
    private String typeName;

//    @ApiModelProperty(value = "下载")
    private Integer download = 0;

//    @ApiModelProperty(value = "编辑")
    private Integer editor = 0;

//    @ApiModelProperty(value = "打印")
    private Integer print = 0;

//    @ApiModelProperty(value = "预览")
    private Integer preview = 0;

//    @ApiModelProperty(value = "上传")
    private Integer upload = 0;

//    @ApiModelProperty(value = "新建")
    private Integer newBuilt = 0;

//    @ApiModelProperty(value = "删除")
    private Integer remove = 0;

//    @ApiModelProperty(value = "查看")
    private Integer query = 0;

//    @ApiModelProperty(value = "复制")
    private Integer copy = 0;

//    @ApiModelProperty(value = "移动")
    private Integer move = 0;

//    @ApiModelProperty(value = "重命名")
    private Integer rechristen = 0;

//    @ApiModelProperty(value = "管理")
    private Integer manage;

//    @ApiModelProperty(value = "权限参数JSON")
    private String params;

//    @ApiModelProperty(value = "是否继承")
    private Integer isExtend;

    private List<EnterprisePermDto> enterprisePermDtos;
    public void setParams(String params) {
        this.params = params;
    }

    private String permCodes;

    private void resetPermCodes(){
        setQuery(0);
        setPrint(0);
        setEditor(0);
        setManage(0);
        setDownload(0);
        setUpload(0);
    }

    public void setPermCodes(String permCodes) {
        if (permCodes != null){
            switch (permCodes){
                case "query":{
                    resetPermCodes();
                    setQuery(1);
                    this.permCodes = permCodes;
                    setParams(permCodes);
                    break;
                }
                case "print":{
                    resetPermCodes();

                    setPrint(1);
                    this.permCodes = permCodes;
                    setParams(permCodes);
                    break;
                }
                case "download":{
                    resetPermCodes();

                    setDownload(1);
                    this.permCodes = permCodes;
                    setParams(permCodes);
                    break;
                }
                case "editor":{
                    resetPermCodes();

                    setEditor(1);
                    this.permCodes = permCodes;
                    setParams(permCodes);
                    break;
                }
                case "upload":{
                    resetPermCodes();

                    setUpload(1);
                    this.permCodes = permCodes;
                    setParams(permCodes);
                    break;
                }
                case "manage":{
                    resetPermCodes();

                    setManage(1);
                    this.permCodes = permCodes;
                    setParams(permCodes);
                    break;
                }
//                default:throw new BadRequestException("参数错误");
            }
        }

    }

//    @ApiModelProperty(value = "属性list")
    List<String> data;

    public void objectChangeList() {
        Field[] declaredFields = this.getClass().getDeclaredFields();
        data = new ArrayList<>();
        for (Field item : declaredFields) {
            String name = item.getName();
            if (StringUtils.equals(name, "serialVersionUID")) {
                continue;
            }
            if (StringUtils.equals(name, "id")) {
                continue;
            }
            if (StringUtils.equals(name, "type")) {
                continue;
            }
            if (StringUtils.equals(name, "typeId")) {
                continue;
            }
            if (StringUtils.equals(name, "typeName")) {
                continue;
            }
            if (StringUtils.equals(name, "treeNodeId")) {
                continue;
            }
            String getMethod = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            try {
                Method method = this.getClass().getMethod(getMethod);
                if (String.valueOf(method.invoke(this)).equals("1"))
                    data.add(name);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }


        }
    }
    public void listChangeObject() {
        Field[] declaredFields = this.getClass().getDeclaredFields();
        for (Field item : declaredFields) {
            String name = item.getName();
            if (StringUtils.equals(name, "serialVersionUID")) {
                continue;
            }
            if (StringUtils.equals(name, "id")) {
                continue;
            }
            if (StringUtils.equals(name, "type")) {
                continue;
            }
            //用户id
            if (StringUtils.equals(name, "typeId")) {
                continue;
            }
            if (StringUtils.equals(name, "typeName")) {
                continue;
            }
            if (StringUtils.equals(name, "treeNodeId")) {
                continue;
            }
            for (int i = 0; i < data.size(); i++) {
                if (StringUtils.equals(data.get(i), name)) {
                    name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
                    try {
                        Method method = this.getClass().getMethod(name, Integer.class);
                        method.invoke(this, 1);
                    } catch (NoSuchMethodException e) {
                        throw new RuntimeException(e);
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                }
            }

        }
    }
/*****************文件夹权限对应赋值*******************/

}
